package com.gduf.lsm.service;

import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.JoinEventsStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.Examine;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.ExamineMapper;
import com.gduf.lsm.pojo.dto.JoinStatusDTO;
import com.gduf.lsm.pojo.vo.ExamineShowVO;
import com.gduf.lsm.pojo.vo.MentalShowVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lsm
 * @Data
 * 文化活动报名表
 */
@Service
public class ExamineService {
    @Autowired
    private ExamineMapper examineMapper;

    /**
     * 新增
     * @param examine
     */
    public int add(Examine examine) {
        //先判断必要字段是否为空
        Boolean flag=ObjectUtil.isNull(examine.getApplyId())||ObjectUtil.isNull(examine.getOldId())||ObjectUtil.isNull(examine.getUserId());
        if(flag){
            throw new CustomException(ResultCodeEnum.EXAMINE_NULL_ERROR);
        }
        //计算身体质量指数
        if(examine.getHeight()!=0&&examine.getWeight()!=0){
            float bmi=examine.getWeight()/(examine.getHeight()*examine.getHeight())*10000;
            //保留一位小数
            float bmiRound=Math.round(bmi*10.0) / 10.0f;
            examine.setBmi(bmiRound);
        }
        return examineMapper.insert(examine);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        examineMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            examineMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param examine
     */
    public void updateById(Examine examine) {
        examineMapper.updateById(examine);
    }

    /**
     * 修改活动状态
     * @param joinStatusDTO
     */
    public void updateStatus(JoinStatusDTO joinStatusDTO) {
        //未通过状态下备注不可为空
        if(joinStatusDTO.getStatus().equals(JoinEventsStatusConstant.JOIN_EVENTS_STATUS_NOT_PASSED)&&ObjectUtil.isEmpty(joinStatusDTO.getRemark())){
            throw new CustomException(ResultCodeEnum.JOIN_ACTIVITY_REMARK_NULL_ERROR);
        }
        //把前端实体拷贝
        Examine examine=new Examine();
        BeanUtils.copyProperties(joinStatusDTO,examine);
        examineMapper.updateById(examine);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Examine selectById(Integer id) {
        return examineMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param examine
     * @return
     */
    public List<Examine> selectAll(Examine examine) {
        return examineMapper.selectAll(examine);
    }

    /**
     * 查询完整的一条信息
     * @param id
     * @return
     */
    public ExamineShowVO selectAllById(Integer id) {
        return examineMapper.selectAllById(id);
    }

    /**
     * 分页查询
     * @param examine
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ExamineShowVO> selectPage(Examine examine, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExamineShowVO> list=examineMapper.selectPageQuery(examine);
        return PageInfo.of(list);
    }

    /**
     * 按用户ID查询参与过的活动，映射VO
     * @param userId
     * @return
     */
    public List<ExamineShowVO> selectByUserId(Integer userId) {
        Examine examine=new Examine();
        examine.setUserId(userId);
        List<ExamineShowVO> list=examineMapper.selectPageQuery(examine);
        return list;
    }
}
